package daw1.programacion.ud12.strategy;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Calendar;

public class StrategyFactory {
	private static StrategyFactory strategyFactory;

	private ISaludoStrategy saludoStrategy;

	@SuppressWarnings("resource")
	private String getClaseACargar() {
		String clase = null;
		FileReader fr = null;
		try {
			fr = new FileReader("src/daw1/programacion/ud12/strategy/estrategia.ini");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		BufferedReader br = new BufferedReader(fr);
		try {
			clase = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return clase;
	}

	public ISaludoStrategy getSaludoStrategy() {
		cargarSaludoStrategyAPartirDeUnFichero();
		//cargarSaludoStrategyCadaMinuto();
		return saludoStrategy;
	}
	
	private void cargarSaludoStrategyCadaMinuto() {
		int minutoActual= Calendar.getInstance().get(Calendar.MINUTE);
		System.out.println("Minuto actual: "+minutoActual); //DEBUG
		if (minutoActual%2==0) {
			saludoStrategy=new SaludoAmableStrategy();
		}
		else {
			saludoStrategy=new SaludoFormalStrategy();
		}
	}
	private void cargarSaludoStrategyAPartirDeUnFichero() {
		if (saludoStrategy == null) {
			String claseACargar = getClaseACargar();
			try {
				saludoStrategy = (ISaludoStrategy) Class.forName(claseACargar)
						.newInstance();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private StrategyFactory() {
	}

	public static StrategyFactory getStrategyFactory() {
		if (strategyFactory == null) {
			strategyFactory = new StrategyFactory();
		}
		return strategyFactory;
	}
}
